package com.jlu.yygh.controller.api;

import com.jlu.yygh.common.exception.CustomException;
import com.jlu.yygh.common.result.Result;
import com.jlu.yygh.common.result.ResultCodeEnum;
import com.jlu.yygh.helper.HttpRequestHelper;
import com.jlu.yygh.model.hosp.Department;
import com.jlu.yygh.model.hosp.Hospital;
import com.jlu.yygh.model.hosp.Schedule;
import com.jlu.yygh.service.DepartmentService;
import com.jlu.yygh.service.HospitalService;
import com.jlu.yygh.service.HospitalSetService;
import com.jlu.yygh.service.ScheduleService;
import com.jlu.yygh.utils.MD5;
import com.jlu.yygh.vo.hosp.DepartmentQueryVo;
import com.jlu.yygh.vo.hosp.ScheduleOrderVo;
import com.jlu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

  @Autowired
  private HospitalService hospitalService;

  @Autowired
  private HospitalSetService hospitalSetService;

  @Autowired
  private DepartmentService departmentService;

  @Autowired
  private ScheduleService scheduleService;

  //上传医院接口
  @ApiOperation(value = "上传医院")
  @PostMapping("saveHospital")
  public Result saveHosp(HttpServletRequest request){

    Map<String, String[]> requestMap = request.getParameterMap();
    Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);

//    1 获取医院系统传递过来的签名，签名进行md5加密
    String hospSign = (String) map.get("sign");

//    2 根据传递过来医院编码，查询数据库，查询签名
    String hoscode = (String) map.get("hoscode");
    String signKey = hospitalSetService.getSignKey(hoscode);

//    3 加密 并比较
//    System.out.println(hospSign);
    String encrypt = MD5.encrypt(signKey);
//    System.out.println(encrypt);
    if (!encrypt.equals(hospSign)){
      throw new CustomException(ResultCodeEnum.SIGN_ERROR);
    }
//    图片转base64优化
    String logoData = (String) map.get("logoData");
    logoData = logoData.replace(" ","+");
    map.put("logoData",logoData);
    hospitalService.save(map);
    return Result.ok();
  }

//  查询医院

  @PostMapping("hospital/show")
  @ApiOperation(value = "查询医院")
  public Result getHospital(HttpServletRequest request){

    Map<String, String[]> requestMap = request.getParameterMap();
    Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);

    String hoscode = (String) map.get("hoscode");

    //    1 获取医院系统传递过来的签名，签名进行md5加密
    String hospSign = (String) map.get("sign");

//    2 根据传递过来医院编码，查询数据库，查询签名
    String signKey = hospitalSetService.getSignKey(hoscode);

//    3 加密 并比较

    String encrypt = MD5.encrypt(signKey);

    if (!encrypt.equals(hospSign)){
      throw new CustomException(ResultCodeEnum.SIGN_ERROR);
    }

    Hospital hospital = hospitalService.getByHoscode(hoscode);

    return Result.ok(hospital);
  }

//  上传科室接口
  @PostMapping("saveDepartment")
  @ApiOperation(value = "上传科室")
  public Result saveDepartment(HttpServletRequest request){
    Map<String, String[]> requestMap = request.getParameterMap();
    Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);

//    1 获取医院系统传递过来的签名，签名进行md5加密
    String hospSign = (String) map.get("sign");

//    2 根据传递过来医院编码，查询数据库，查询签名
    String hoscode = (String) map.get("hoscode");
    String signKey = hospitalSetService.getSignKey(hoscode);

//    3 加密 并比较
//    System.out.println(hospSign);
    String encrypt = MD5.encrypt(signKey);
//    System.out.println(encrypt);
    if (!encrypt.equals(hospSign)){
      throw new CustomException(ResultCodeEnum.SIGN_ERROR);
    }
    departmentService.save(map);
    return Result.ok();
  }

  @ApiOperation(value = "获取分页列表")
  @PostMapping("department/list")
  public Result department(HttpServletRequest request) {
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

//    医院编号
    String hoscode = (String)paramMap.get("hoscode");

    int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
    int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

//    1 获取医院系统传递过来的签名，签名进行md5加密
    String hospSign = (String) paramMap.get("sign");

//    2 根据传递过来医院编码，查询数据库，查询签名
    String signKey = hospitalSetService.getSignKey(hoscode);

//    3 加密 并比较
    String encrypt = MD5.encrypt(signKey);
    if (!encrypt.equals(hospSign)){
      throw new CustomException(ResultCodeEnum.SIGN_ERROR);
    }

    DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
    departmentQueryVo.setHoscode(hoscode);
    Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
    return Result.ok(pageModel);
  }

  @ApiOperation(value = "删除科室")
  @PostMapping("department/remove")
  public Result removeDepartment(HttpServletRequest request) {
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//    医院编号
    String hoscode = (String)paramMap.get("hoscode");
//    科室编号
    String depcode = (String)paramMap.get("depcode");

//    1 获取医院系统传递过来的签名，签名进行md5加密
    String hospSign = (String) paramMap.get("sign");

//    2 根据传递过来医院编码，查询数据库，查询签名
    String signKey = hospitalSetService.getSignKey(hoscode);

//    3 加密 并比较
    String encrypt = MD5.encrypt(signKey);
    if (!encrypt.equals(hospSign)){
      throw new CustomException(ResultCodeEnum.SIGN_ERROR);
    }
    departmentService.remove(hoscode, depcode);
    return Result.ok();
  }

  @ApiOperation(value = "上传排班")
  @PostMapping("/saveSchedule")
  public Result saveSchedule(HttpServletRequest request){

    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

//    1 获取医院系统传递过来的签名，签名进行md5加密
    String hospSign = (String) paramMap.get("sign");

//    2 根据传递过来医院编码，查询数据库，查询签名
    String signKey = hospitalSetService.getSignKey((String) paramMap.get("hoscode"));

//    3 加密 并比较
    String encrypt = MD5.encrypt(signKey);
    if (!encrypt.equals(hospSign)){
      throw new CustomException(ResultCodeEnum.SIGN_ERROR);
    }

    scheduleService.save(paramMap);

    return Result.ok();
  }

  @ApiOperation(value = "查询排班")
  @PostMapping("/schedule/list")
  public Result scheduleList(HttpServletRequest request){

    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

    int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
    int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
//    医院编号
    String hoscode = (String)paramMap.get("hoscode");
//    非必填
    String depcode = (String)paramMap.get("depcode");

//    1 获取医院系统传递过来的签名，签名进行md5加密
    String hospSign = (String) paramMap.get("sign");

//    2 根据传递过来医院编码，查询数据库，查询签名
    String signKey = hospitalSetService.getSignKey(hoscode);

//    3 加密 并比较
    String encrypt = MD5.encrypt(signKey);
    if (!encrypt.equals(hospSign)){
      throw new CustomException(ResultCodeEnum.SIGN_ERROR);
    }

    ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
    scheduleQueryVo.setHoscode(hoscode);
    scheduleQueryVo.setDepcode(depcode);
    Page<Schedule> pageModel = scheduleService.selectPage(page, limit, scheduleQueryVo);
    return Result.ok(pageModel);
  }

  @ApiOperation(value = "删除科室")
  @PostMapping("schedule/remove")
  public Result removeSchedule(HttpServletRequest request) {
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//   医院编号
    String hoscode = (String)paramMap.get("hoscode");
//必填
    String hosScheduleId = (String)paramMap.get("hosScheduleId");
//    1 获取医院系统传递过来的签名，签名进行md5加密
    String hospSign = (String) paramMap.get("sign");

//    2 根据传递过来医院编码，查询数据库，查询签名
    String signKey = hospitalSetService.getSignKey(hoscode);

//    3 加密 并比较
    String encrypt = MD5.encrypt(signKey);
    if (!encrypt.equals(hospSign)){
      throw new CustomException(ResultCodeEnum.SIGN_ERROR);
    }
    scheduleService.remove(hoscode, hosScheduleId);
    return Result.ok();
  }
}
